package com.qire.manhua.controller;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;

import androidx.annotation.AnimRes;
import androidx.annotation.NonNull;

import com.qire.manhua.R;
import com.qire.manhua.activity.ImagePickerActivity;
import com.qire.manhua.databinding.ActivityImagePickerBinding;
import com.qire.manhua.databinding.LayoutImagePickerSelectedItemBinding;
import com.qire.manhua.model.viewModel.impl.ImagePickerViewModel;
import com.qire.manhua.model.viewModel.impl.ImagePickerViewModel.AlbumListItemModel;
import com.qire.manhua.model.viewModel.impl.ImagePickerViewModel.GridPictureItemModel;
import com.qire.manhua.model.viewModel.impl.ImagePickerViewModel.SelectBitmapItemModel;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.support.base.EventController;
import com.qire.common.utils.ToastHelper;

import java.util.ArrayList;

public class ImagePickerController extends EventController<ImagePickerActivity> implements AutoVariable<LayoutImagePickerSelectedItemBinding> {

    private ImagePickerViewModel viewModel;

    public ImagePickerController(@NonNull ImagePickerActivity owner) {
        super(owner);
    }

    @Override
    protected void bindView() {
        ActivityImagePickerBinding viewDataBinder = owner.viewDataBinder();
        viewDataBinder.setImagePickerController(this);
    }

    @Override
    public void bindVariable(LayoutImagePickerSelectedItemBinding viewBind) {
        viewBind.setImagePickerController(this);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(ImagePickerViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    public void backBtnOnClick(View view) {
        owner.finish();
    }

    /**
     * 下一步单击事件*/
    public void startTextOnclick(View view) {
        if(viewModel.selectDataList.size() < viewModel.min_limit) {
            String content = String.format("至少选择%d张图片", viewModel.min_limit);
            ToastHelper.showToast(content);
            return;
        }

        if (viewModel.selectDataList.size() <= 0) {
            ToastHelper.showToast("请选择一张图片");
            return;
        }

        // 从选中对象中获取选择路径集合，并传递给唤起页面
//        String pickerPath = viewModel.selectDataList.get(0).itemSelectImage.getUrl();
        ArrayList<String> pickerPathList = new ArrayList<>();
        for(SelectBitmapItemModel itemModel : viewModel.selectDataList) {
            pickerPathList.add(itemModel.itemSelectImage.getUrl());
        }
        owner.notifyResultsToParent((ArrayList<String>) pickerPathList.clone());
    }

    /**
     * 已选图片选项单击事件*/
    public void selectImageOnItemClick(View view) {
        SelectBitmapItemModel itemModel = (SelectBitmapItemModel) view.getTag();
        viewModel.removeSelectBitmapItem(itemModel.gridPictureItemModel);
    }

    /**
     * 选择图片选项单击事件*/
    public void pictureItemOnClick(AdapterView<?> parent, View view, int position, long id) {

        GridPictureItemModel itemModel = viewModel.pictureDataList.get(position);

        if(itemModel.checkSelected()) {
            viewModel.removeSelectBitmapItem(itemModel);
        } else {
            if (viewModel.checkLimit()) {
                String content = String.format("最多选择%d张照片", viewModel.max_limit);
                ToastHelper.showToast(content);
                return;
            }
            viewModel.addSelectBitmapItem(itemModel);
        }

    }

    /**
     * 打开图片目录单击事件*/
    public void folderViewOnItemClick(AdapterView<?> parent, View view, int position, long id) {
        final ActivityImagePickerBinding viewDataBinder = owner.viewDataBinder();

        viewModel.setCurrentImageFolder(viewModel.mDirPaths.get(position));
        hideListAnimation(200, viewDataBinder.listViewLayout, R.anim.app_anim_fade_out, animListener);
        viewModel.pictureAdapter.notifyDataSetChanged();

        for(AlbumListItemModel itemModel : viewModel.folderDataList) {
            itemModel.setChooseVisible(View.GONE);
        }

        viewModel.folderDataList.get(position).setChooseVisible(View.VISIBLE);
        viewModel.updateAlbumTitle("" + viewModel.getCurrentImageFolder().getName());
    }

    /**
     * 图片目录列表选项单击事件*/
    public void selectDirOnclick(View view) {
        final ActivityImagePickerBinding viewDataBinder = owner.viewDataBinder();

        if (isVisible(viewDataBinder.listView)) {
            hideListAnimation(200, viewDataBinder.listViewLayout, R.anim.app_anim_fade_out, animListener);
        } else {
            setViewVisibilityState(viewDataBinder.listViewLayout, View.VISIBLE);
            setViewVisibilityState(viewDataBinder.listView, View.VISIBLE);
            showListAnimation(400, R.anim.app_image_picker_anim_album);
            viewDataBinder.getImagePickerViewModel().folderAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 图片目录背景事件*/
    public void dirLayoutOnclick(View view) {
        final ActivityImagePickerBinding viewDataBinder = owner.viewDataBinder();

        if (isVisible(viewDataBinder.listView)) {
            hideListAnimation(200, viewDataBinder.listViewLayout, R.anim.app_anim_fade_out, animListener);
        }
    }

    private boolean isVisible(View view) {
        if(view != null && view.getVisibility() == View.VISIBLE) {
            return true;
        }
        return false;
    }

    private void setViewVisibilityState(View view, int visibility) {
        if(visibility == View.VISIBLE || visibility == View.GONE || visibility == View.INVISIBLE) {
            if(view != null && view.getVisibility() != visibility) {
                view.setVisibility(visibility);
            }
        }
    }

    private void showListAnimation(long time, @AnimRes int animId) {
        Animation anim = AnimationUtils.loadAnimation(owner, animId);
        anim.setDuration(time);
        owner.viewDataBinder().listView.startAnimation(anim);
    }

    private void hideListAnimation(long time, View view, @AnimRes int animId, Animation.AnimationListener animListener) {
        Animation anim = AnimationUtils.loadAnimation(owner, animId);
        anim.setDuration(time);
        view.startAnimation(anim);
        anim.setAnimationListener(animListener);
    }

    Animation.AnimationListener animListener = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            final ActivityImagePickerBinding viewDataBinder = owner.viewDataBinder();
            viewDataBinder.listView.setVisibility(View.GONE);
            viewDataBinder.listViewLayout.setVisibility(View.GONE);
        }
    };
}